package com.lightningtrade.controller;

import com.lightningtrade.domain.SysFlashSale;
import com.lightningtrade.service.SysFlashSaleService;
import com.lightningtrade.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "秒杀活动管理-0312杜晟玮")
@RestController
@RequestMapping("/flashSale")
public class SysFlashSaleController {

    @Autowired
    private SysFlashSaleService sysFlashSaleService;

    @ApiOperation(value = "新增秒杀活动", notes = "添加一个新的秒杀活动", response = AjaxResult.class)
    @PostMapping("/add")
    public AjaxResult addFlashSale(@RequestBody SysFlashSale flashSale) {
        boolean result = sysFlashSaleService.addFlashSale(flashSale);
        if (result) {
            return AjaxResult.success("新增秒杀活动成功");
        } else {
            return AjaxResult.error("新增秒杀活动失败");
        }
    }

    @ApiOperation(value = "更新秒杀活动", notes = "根据秒杀活动ID更新秒杀活动信息", response = AjaxResult.class)
    @PutMapping("/update")
    public AjaxResult updateFlashSale(@RequestBody SysFlashSale flashSale) {
        boolean result = sysFlashSaleService.updateFlashSale(flashSale);
        if (result) {
            return AjaxResult.success("更新秒杀活动成功");
        } else {
            return AjaxResult.error("更新秒杀活动失败");
        }
    }

    @ApiOperation(value = "删除秒杀活动", notes = "根据秒杀活动ID删除秒杀活动", response = AjaxResult.class)
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteFlashSale(@PathVariable int id) {
        boolean result = sysFlashSaleService.deleteFlashSale(id);
        if (result) {
            return AjaxResult.success("删除秒杀活动成功");
        } else {
            return AjaxResult.error("删除秒杀活动失败");
        }
    }

    @ApiOperation(value = "根据ID查询秒杀活动", notes = "根据秒杀活动ID查询秒杀活动信息", response = SysFlashSale.class)
    @GetMapping("/get/{id}")
    public AjaxResult getFlashSaleById(@PathVariable int id) {
        SysFlashSale flashSale = sysFlashSaleService.getFlashSaleById(id);
        if (flashSale != null) {
            return AjaxResult.success(flashSale);
        } else {
            return AjaxResult.error("未找到秒杀活动");
        }
    }

    @ApiOperation(value = "获取所有秒杀活动", notes = "获取所有的秒杀活动", response = List.class)
    @GetMapping("/getAll")
    public AjaxResult getAllFlashSales() {
        List<SysFlashSale> flashSales = sysFlashSaleService.getAllFlashSales();
        return AjaxResult.success(flashSales);
    }
}
